package aom.ju.ss.view.user.shop.management.order

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import aom.ju.ss.R
import aom.ju.ss.databinding.ListBinding
import aom.ju.ss.databinding.OrderListFragmentBinding
import aom.ju.ss.databinding.OrderListItemFragmentItemBinding
import aom.ju.ss.databinding.OrderManageBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.ShopOrderList
import aom.ju.ss.mode.ShopOrderListData
import aom.ju.ss.mode.ShopOrderListDetails
import aom.ju.ss.utils.copy
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.pay.WxXcxzf
import aom.ju.ss.utils.show
import aom.ju.ss.utils.start
import aom.ju.ss.utils.startResult
import aom.ju.ss.view.base.*
import aom.ju.ss.view.main.LpViewModel
import aom.ju.ss.view.pay.PayActivity
import com.google.gson.Gson

class OrderListItemFragment : BaseListFragment() {

    companion object {
        fun getMy(type: String): OrderListItemFragment {
            val f = OrderListItemFragment()
            val b = Bundle()
            b.putString("type", type)
            f.arguments = b
            return f
        }
    }

    override fun item(): Int {
        return R.layout.order_list_item_fragment_item
    }

    override fun bind(b: ViewDataBinding, position: Int) {
        super.bind(b, position)
        val ib = b as OrderListItemFragmentItemBinding
        val info = getList()[position] as ShopOrderListData
        ib.info = info
        ib.rootView.setOnClickListener { }

        ib.bt.setOnClickListener {
            if (info.status == 2) {//|| info.status == 3
                mContext!!.startResult(
                    PostGoods::class.java,
                    1993,
                    info.goods_list.goods_id.toString()
                )
            } else if (info.status == 3) {
//                OKHttpUtils(activity)
//                    .SetApiUrl(LP_API.shop_order_order_info)
//                    .SetKey("id")
//                    .SetValue(info.id)
//                    .POST(object : MyHttpCallBack {
//                        override fun ok(jsonString: String?, httpTY: String?) {
//                            val mode = Gson().fromJson(jsonString, ShopOrderListDetails::class.java)
//
//                        }
//
//                        override fun error(e: String?, jsonString: String?, httpTY: String?) {
//                            e?.show
//                        }
//
//                    })
                copy(mContext!!, "info.courier_no")
                "快递单号已复制".show
                WebViewActivity.startWeb(mContext!!, "物流查询", "https://www.kuaidi100.com/?from=openv")
            } else {
                ib.rootView.performClick()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1993) {
                binding?.small?.autoRefresh()
            }
        }
    }

    override fun api(): String {
        return LP_API.shop_order_order_list
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ShopOrderList::class.java)
        setList(mode.data)

    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show
    }

    override fun key(): Array<String> {
        return arrayOf("type", "status", "page")
    }

    override fun value(): Array<String> {
        return arrayOf("0", requireArguments().getString("type", ""), page.toString())
    }

    override fun onBaseCreateView() {
        super.onBaseCreateView()
        binding!!.rootView.setBackgroundResource(R.color.color_f5f5f5)

    }


}